/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx;

import java.awt.Color;
import rpg.util.MathUtil;

/**
 *
 * @author rothens
 */
public class HSVGradientColor {

    public enum GradientType {

        H,
        S,
        V
    }
    public GradientType type;
    public int h;
    public int s;
    public int v;
    public int to;
    public boolean horizontal;
    double shades;
    int desc;

    public HSVGradientColor(GradientType type, int h, int s, int v, int to, boolean horizontal) {
        this.type = type;
        this.h = MathUtil.clamp(0, 360, h);
        this.s = MathUtil.clamp(0, 100, s);
        this.v = MathUtil.clamp(0, 100, v);
        this.to = MathUtil.clamp(0, (type == GradientType.H) ? 360 : 100, to);
        this.horizontal = horizontal;
        switch (type) {
            case H:
                shades = Math.abs(to - h);
                desc = MathUtil.signum(to - h);
                break;
            case S:
                shades = Math.abs(to - s);
                desc = MathUtil.signum(to - h);
                break;
            case V:
                shades = Math.abs(to - v);
                desc = MathUtil.signum(to - h);
                break;
        }
    }

    public Bitmap createGradient(Bitmap b) {
        shades++;
        int size = (int) Math.ceil((horizontal) ? b.width / shades : b.height / shades);
        for (int i = 0; i < shades; i++) {
            int c = 0;
            switch (type) {
                case H:
                    c = Color.HSBtoRGB((h + i * desc) / 360f, s / 100f, v / 100f);
                    break;
                case S:
                    c = Color.HSBtoRGB(h / 360f, (s + i * desc) / 100f, v / 100f);
                    break;
                case V:
                    c = Color.HSBtoRGB(h / 360f, s / 100f, (v + i * desc) / 100f);
                    break;

            }

            b.fill(c, (horizontal) ? i * size : 0, (horizontal) ? 0 : i * size, (horizontal) ? (i + 1) * size : b.width, (horizontal) ? b.height : (i + 1) * size);
        }
        return b;
    }
}
